package com.davidhigenbottam.HeatMap.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CheckBox;

public class InstitutionsCheckBox extends CheckBox {
	
	
	public static final String INSTITUTION_KML_URL="http://studentheatmap.appspot.com/institutions.kml";

	KmlLayer kmlOverlay= null;
	public InstitutionsCheckBox() {
		super("Show Higher Education Institutions");
		this.setWidth("160px");
		
		this.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				HeatMap.info("InstitutionsCheckBox:onClick:");
				UI.getInstance().showInstitutionKey();
				if(InstitutionsCheckBox.this.getValue()){
					if(kmlOverlay==null)
						loadOverlay();
					else{
						kmlOverlay.setVisible(true);
						
					}
				}
				else{
					UI.getInstance().hideInstitutionKey();
					if(kmlOverlay!=null)
						kmlOverlay.setVisible(false);
				}
				
			}});

	}
	
	private void loadOverlay(){
		this.kmlOverlay= UI.getInstance().getMap().createKmlLayer(INSTITUTION_KML_URL);
//		GeoXmlOverlay.load(INSTITUTION_KML_URL, new GeoXmlLoadCallback() {
//																
//			@Override
//			public void onFailure(String url, Throwable e) {
//				HeatMap.error("GeoXmlOverlay.load:onFailure after KML File " + url
//							+ " failed to load",e);
//	
//			}
//
//			@Override
//			public void onSuccess(String url, GeoXmlOverlay geoXmlOverlay) {
//				
//				UI.getInstance().getMap().addOverlay(geoXmlOverlay);
//				InstitutionsCheckBox.this.geoXmlOverlay= geoXmlOverlay;
//				
//			}
//
//		});		
		
		UI.getInstance().addKmlUrl(INSTITUTION_KML_URL, "institutions");
		
	}

	
}
